#include "PcapMsg.h"
#include "PcapExceptions.h"


namespace Pacap {


PackType PcapMsg::pack()
{
    throw ExceptUndefineMethod("this method is not coded");
    //先判断两个指针是否为空
    //把两个 pack 相加
    //返回相加结果
}

void PcapMsg::unpack(PackType hexData)
{
    if( hexData.length() < 14+InetIpv4::getLENGTH()+InetTcp::getLENGTH()){
        throw ExceptUncorrectLength("the length of hexData is too small");
    }
//    if( nullptr == Ethernet ){
//        throw ExceptUncorrectStep("please setEthernet first");
//    }
    if( nullptr == InternetIp ){
        throw ExceptUncorrectStep("please setInternetIp first");
    }
    if(nullptr == TcpProtocol){
        throw ExceptUncorrectLength("please setTcpProtocol first");
    }

    //解析Ethernet部分
  //  Ethernet->unpack({(uint8_t*)hexData.data(),PcapEthernet::getLENGTH()});
    //InternetIp
    InternetIp->unpack({(uint8_t*)hexData.data()+14,hexData.length()-14});
    //TCPProtocol
    TcpProtocol->unpack(InternetIp->getData());
    applicationData = TcpProtocol->getData();
}
///**
// * @brief PcapMsg::getEthernet
// * @return
// */
//PcapEthernet *PcapMsg::getEthernet() const
//{
//    return Ethernet;
//}
///**
// * @brief PcapMsg::setEthernet
// * @param value
// */
//void PcapMsg::setEthernet(PcapEthernet *value)
//{
//    Ethernet = value;
//}
/**
 * @brief PcapMsg::getInternetIp
 * @return
 */
InetIpv4 *PcapMsg::getInternetIp() const
{
    return InternetIp;
}
/**
 * @brief PcapMsg::setInternetIp
 * @param value
 */
void PcapMsg::setInternetIp(InetIpv4 *value)
{
    InternetIp = value;
}
/**
 * @brief PcapMsg::getTcp
 * @return
 */
InetTcp *PcapMsg::getTcp() const
{
    return TcpProtocol;
}
/**
 * @brief PcapMsg::setTcp
 * @param value
 */
void PcapMsg::setTcp(InetTcp *value)
{
    TcpProtocol = value;
}

PackType PcapMsg::getApplicationData() const
{
    return applicationData;
}

void PcapMsg::setApplicationData(const PackType &value)
{
    applicationData = value;
}


}
